package com.ywjt.platformbusiness.login;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.constants.Constants;
import com.ywjt.platformbusiness.main.MainActivity;
import com.ywjt.platformbusiness.util.SPUtils;
import com.ywjt.platformbusiness.util.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 启动页
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class StartActivity extends BaseActivity {
    private static final int GO_HOME = 1;
    private static final int GO_GUIDE = 2;
    // 延迟2秒
    private static final long SPLASH_DELAY_MILLIS = 2000;
    private ImageView ivStartImg;
    private int versionCode=1;//当前版本号

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(false);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_start;
    }

    @Override
    protected void initView() {
        ivStartImg=findViewById(R.id.ivStartImg);
        ivStartImg.setImageResource(R.mipmap.ic_launcher);
    }

    @Override
    protected void initData() {
       // versionCode = PackageInfoUtils.getVersionCode(getContext());
        if (SPUtils.getSharedBooleanData(StartActivity.this, "isLogin")) {//非首次打开app
//            String shareCode = SharedPreferencesUtils.getSharedPreferences(getContext(), Constants.ShareKey_Version);
//            if (shareCode.equals(String.valueOf(versionCode))) {//跳转至首页
               goHome();
//            } else {//版本升级后首次登录，跳转至导航界面
//                goGuide();
            //}
           } else {// 首次打开app
           // SPUtils.setSharedBooleanData(StartActivity.this,"isLogin",true);
            //goGuide();
            LoginActivity.actionStart(StartActivity.this);
            finish();
        }
    }

    @Override
    public int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    private void goHome() {
        handler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
    }

    private void goGuide() {
        handler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
        Map<String, String> map = new HashMap<>();
        map.put(Constants.ShareKey_Version, String.valueOf(versionCode));
        SharedPreferencesUtils.saveSharedPreferences(getContext(), map);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    MainActivity.actionStart(getContext(),0);
                    finish();
                    break;
                case GO_GUIDE:
                   // GuideActivity.actionStart(getContext());
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}